/*=========================================================================

  Library:   CFS

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

// Qt includes

// CFS includes
#include "CFSBooleanMapper.h"

//-----------------------------------------------------------------------------
class CFSBooleanMapperPrivate
{
public:
  CFSBooleanMapperPrivate();
  QByteArray PropertyName;
  QVariant TrueValue;
  QVariant FalseValue;
};

// --------------------------------------------------------------------------
CFSBooleanMapperPrivate::CFSBooleanMapperPrivate()
{
  this->TrueValue = QVariant(true);
  this->FalseValue = QVariant(false);
}

// --------------------------------------------------------------------------
// CFSBooleanMapper methods

// --------------------------------------------------------------------------
CFSBooleanMapper::CFSBooleanMapper(
  QObject* targetObject, const QByteArray& property, const QByteArray& signal)
  : QObject(targetObject)
  , d_ptr(new CFSBooleanMapperPrivate)
{
  Q_ASSERT(property.isEmpty() != true);
  Q_ASSERT(targetObject != 0);
  Q_D(CFSBooleanMapper);
  d->PropertyName = property;
  if (!signal.isEmpty())
    {
    connect(targetObject, signal, this, SLOT(emitValueChanged()));
    }
}

// --------------------------------------------------------------------------
CFSBooleanMapper::~CFSBooleanMapper()
{
}

// --------------------------------------------------------------------------
QByteArray CFSBooleanMapper::propertyName()const
{
  Q_D(const CFSBooleanMapper);
  return d->PropertyName;
}

// --------------------------------------------------------------------------
QObject* CFSBooleanMapper::targetObject()const
{
  return this->parent();
}

// --------------------------------------------------------------------------
QVariant CFSBooleanMapper::trueValue()const
{
  Q_D(const CFSBooleanMapper);
  return d->TrueValue;
}

// --------------------------------------------------------------------------
QVariant CFSBooleanMapper::falseValue()const
{
  Q_D(const CFSBooleanMapper);
  return d->FalseValue;
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setTrueValue(const QVariant& trueValue)
{
  Q_D(CFSBooleanMapper);
  if (d->TrueValue == trueValue)
    {
    return;
    }
  d->TrueValue = trueValue;
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setFalseValue(const QVariant& falseValue)
{
  Q_D(CFSBooleanMapper);
  if (d->FalseValue == falseValue)
    {
    return;
    }
  d->FalseValue = falseValue;
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
bool CFSBooleanMapper::value()const
{
  return this->targetObject()->property(this->propertyName()).toBool();
}

// --------------------------------------------------------------------------
bool CFSBooleanMapper::complement()const
{
  return !this->value();
}

// --------------------------------------------------------------------------
int CFSBooleanMapper::valueAsInt()const
{
  Q_D(const CFSBooleanMapper);
  return this->value() ? d->TrueValue.toInt() : d->FalseValue.toInt();
}

// --------------------------------------------------------------------------
QString CFSBooleanMapper::valueAsString()const
{
  Q_D(const CFSBooleanMapper);
  return this->value() ? d->TrueValue.toString() : d->FalseValue.toString();
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setValue(bool value)
{
  this->targetObject()->setProperty(this->propertyName(), QVariant(value));
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setComplement(bool value)
{
  this->setValue(!value);
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setValueAsInt(int intValue)
{
  Q_D(CFSBooleanMapper);
  this->setValue( QVariant(intValue) == d->TrueValue);
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::setValueAsString(const QString& stringValue)
{
  Q_D(CFSBooleanMapper);
  this->setValue( QVariant(stringValue) == d->TrueValue );
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::toggle()
{
  this->setValue(this->complement());
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::emitValueChanged()
{
  emit valueChanged(this->value());
  emit complementChanged(this->complement());
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
void CFSBooleanMapper::emitValueAsChanged()
{
  emit valueAsIntChanged(this->valueAsInt());
  emit valueAsStringChanged(this->valueAsString());
}
